#include "DigitalOut.h"
//#include "Input.h"
#include "Control.h"
#include "EEPROM.h"
#include "Alarm.h"
#include "Timer.h"
#include "JD_Lighting.h"
#include "IDriver.h"
#include "ModbusRegister.h"
#include "JD_InOut.h"
#include "IDriver.h"
#include "Register.h"
#include "MultipleAlarmClocks.h"



/*
 * Function Name:   Comm_Control
 * Description:  
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
void ManualControl()
{
	
	if(Controller.light.Manual == 1)
	{
		SetLightOn();
	}
	else
	{
		SetLightOff();
	}
	
	
}
/*
 * Function Name:   Timer_Control
 * Description: 
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
void TimerControl()
{
u8  lingtTimerValue =	GetLightStartCondition();
	
	
	if((lingtTimerValue)&&(Controller.light.TimerStatus == 0))
	{
		Controller.light.TimerStatus = 1 ;
	}
	else if((lingtTimerValue == 0)&&(Controller.light.TimerStatus == 1))
	{
		
		Controller.light.TimerStatus =0 ;
	}
	
		
	switch(Controller.light.Stage)
	{
		case 0:
		if(Controller.light.TimerStatus == 1 )
	  {
	   	Controller.light.Stage =1;
			Controller.light.Manual = 1;
	  }
			 ManualControl();
		break;
		case 1:
	  	SetLightOn();
		  Controller.light.keyFlag = 0;	
			Controller.light.Stage =2;
		break;
	  case 2:
			if((Controller.light.Manual != 1)&&(Controller.light.keyFlag ==0 ))
			{
			 Controller.light.keyFlag = 1;			
			}
		  if(Controller.light.TimerStatus == 0 )
		  {
		   	Controller.light.Stage =0;
//				if((Controller.light.Manual == 1)&&(Controller.light.keyFlag != 1))
//				{
	          SetLightOff();
				Controller.light.Manual = 0;
//				}
		  }
			if(Controller.light.keyFlag ==1 )
			{
				 ManualControl();			
			}
		 break;

			
	}
}


void ManualTimerControl()
{
	u8  lingtTimerValue =	GetLightStartCondition();
	
	
	if((lingtTimerValue)&&(Controller.light.TimerStatus == 0))
	{
		Controller.light.TimerStatus = 1 ;
	}
	else if((lingtTimerValue == 0)&&(Controller.light.TimerStatus == 1))
	{
		
		Controller.light.TimerStatus =0 ;
	}
	
		
if((lingtTimerValue)&&(Controller.light.TimerStatus == 0))
	{
		Controller.light.TimerStatus = 1 ;
	}
	else if((lingtTimerValue == 0)&&(Controller.light.TimerStatus == 1))
	{
		
		Controller.light.TimerStatus =0 ;
	}
	
		
	switch(Controller.light.Stage)
	{
		case 0:
		if(Controller.light.TimerStatus == 1 )
	  {
	   	Controller.light.Stage =1;
			Controller.light.Manual = 1;
	  }
			 ManualControl();
		break;
		case 1:
	  	SetLightOn();
		  Controller.light.keyFlag = 0;	
			Controller.light.Stage =2;
		break;
	  case 2:
			if((Controller.light.Manual != 1)&&(Controller.light.keyFlag ==0 ))
			{
			 Controller.light.keyFlag = 1;			
			}
		  if(Controller.light.TimerStatus == 0 )
		  {
		   	Controller.light.Stage =0;
//				if((Controller.light.Manual == 1)&&(Controller.light.keyFlag != 1))
//				{
	          SetLightOff();
				Controller.light.Manual = 0;
//				}
		  }
			if(Controller.light.keyFlag ==1 )
			{
				 ManualControl();			
			}
		 break;

			
	}

}


/*
 * Function Name:   JD_LightingControl
 * Description:  ??1??
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
void JD_LightingControl()
{
	u8 workMode = GetJDParamValue(HREG_LIGHTING_MODE);
	u8 value = GetJDParamValue(HREG_LINGTING_COMM);
	
	if(value&0x02)
	{
		
	}
	
	

	if(0 == workMode)
	{
		ManualControl();
	}
	else	if(1 == workMode)
	{
		TimerControl();
	}
	else
	{
		ManualTimerControl();
	}

  
}


